#ifndef SERIALASSISTANT_H
#define SERIALASSISTANT_H

#include <QWidget>
#include <QTimer>
#include <QTime>
#include <QFile>
#include <QTextStream>
#include "baseserialcomm.h"
#include <QStyleFactory>
#include <QMenu>
#include <QTextCodec>
//#include <QFile>
#include <QFileDialog>
#include <QSettings>
//QT_BEGIN_NAMESPACE
namespace Ui { class serialAssistant; }
//QT_END_NAMESPACE

class serialAssistant : public QWidget
{
    Q_OBJECT

public:
    enum VerifyStyle{
        AddVerifyItem      = 0,
        ADD8               = 1,
        NADD8              = 2,
        XOR8               = 3,
        LRC                = 4, // Longitudinal Redundancy Check
        CRC8_MAXIM_DS18B20 = 5,
        CRC16_USB          = 6,
        CRC16_MODBUS       = 7,
        CRC16_CCITT        = 8,
        CRC16_CCITT_FALSE  = 9,
        CRC32              = 10,
        UnknownStyle       = -1
    };
    Q_ENUM(VerifyStyle)

    explicit serialAssistant(QWidget *parent = nullptr);
    ~serialAssistant();

    static bool rxPause;
    static qint32 const pgsBarUpRate = 100;
    static qint32 pgsBarInc;
    QList<QString> saveText;
    QList<QString> saveArray;
    QTimer txTimer;

    QFile *saveFile;
    QFile *loadFile;
    QTextStream *txtStreamSave = NULL;

    baseserialcomm *currentPort;
    QByteArray rxBuffer;
    qint32 rxCount = 0;
    qint32 txCount = 0;
    QVector <QPushButton *> multiPushButton;
    QVector <QLineEdit *> multiTxtLine;
    QVector <QCheckBox *> multiCheckBox;
    quint32 multiGroupCount = 10;

    qint32 rxBufSize = 1024*1024;
    QTimer txtFlashTime;
    bool rxFlag = false;
    QTime rxTimeStamp;

    void insertTimeStramp(QByteArray &tmp);
    void initComboBox_Config();
    void configPort();
    void deleteTxTimer();
    void startTxTimer();
    QByteArray stringToSend(QString src,bool txInHex, bool &ok);
    QByteArray insertVerity(QByteArray &tmp);
    void listVerifyStyle (QComboBox *cbbVerifyStyle);
    void initMultiBtn(QPushButton *btn, quint16 count);
    void renameTxt(QLineEdit *txt, quint16 count);
    void renameCkb(QCheckBox *ckb,quint16 count);
    void loadDefaultConfig();


signals:
    void signals_displayTxBuffer(QByteArray);


public slots:
    void slots_serialRxCallback();
    void slots_errorHandler(QSerialPort::SerialPortError error);
    void slots_highlightLine();
    void slots_timeOutTx();
    void slots_multiSend();
    void slots_timeOutProgressBar();
    void slots_displayTxBuffer(QByteArray);
    void slots_rxDisplayTxt();
//    void slots_showBtnRightClickMenu(const QPoint &pos);


private slots:
    void on_btnOpenPort_clicked();

    void on_btnRefresh_clicked();

    void on_chkFlowCtrlDTR_toggled(bool checked);

    void on_chkFlowCtrlRTS_toggled(bool checked);

    void on_btnSaveFile_clicked();

    void on_btnLoadFile_clicked();

    void on_textBrower_textChanged();

    void on_btnSend_clicked();

    void on_txtSingle_textChanged();

    void on_chbTimedTx_clicked(bool checked);

    void on_spbTxInterval_editingFinished();

    void on_chbTxHex_clicked(bool checked);

    void on_btnTxFile_clicked(bool checked);

    void on_btnClear_clicked();

    void on_pushButton_clicked();

    void on_btnAddNewGroup_clicked();

    void on_btnDelLastGroup_clicked();

//    void on_actionRename_triggered();

    void on_btnSaveGroupData_clicked();

    void on_btnLoadGroupData_clicked();

protected:
    void closeEvent(QCloseEvent *event);


private:
    QMenu *btnMenu;
    Ui::serialAssistant *ui;
};
#endif // SERIALASSISTANT_H
